<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
            <title>Cindy JS</title>
            <script type="text/javascript" src="../build/js/Cindy.js"></script>
            <link rel="stylesheet" href="../css/cindy.css">


            </head>

	<body style="font-family:Arial;"  >

        <h1>CindyJS: Euler-Line</h1>

        <script id='down' type='text/x-cindyscript'>
        loc=Pos.xy;
        </script>
        <script id='up' type='text/x-cindyscript'>
        off=off+Pos.xy-loc;
        Pos.xy=(0,0);
        </script>

        <script id='init' type='text/x-cindyscript'>
loc=Pos.xy;

off=(4,8);
off=(3,3);
g(x,y):=(-x_1*y_1+x_2*y_2+x_3*y_3+x_4*y_4-x_5*y_5);
lip(x,y):=((1+|(x,y)|^2)/2,(1-|(x,y)|^2)/2,x,y,0);
lic(x,y,r):=[(1+|[x,y]|^2-r^2)/2,(1-|[x,y]|^2+r^2)/2,x,y,r];
lig(x,y,z):=(
  pp=(x,y,z)/|(x,y)|;
  (-pp_3,pp_3,pp_1,pp_2,1);
);

rev(p):=(p_1,p_2,p_3,p_4,-p_5);

liec(x,y,r):=(
  erg=lic(x,y,r);
  erg=erg/|erg|;
  erg;
);
liec(v,r):=(
  erg=lic(v.x,v.y,r);
  erg=erg/|erg|;
  erg;
);

lieg(x,y,z):=(
  erg=lig(x,y,z);
  erg=erg/|erg|;
  erg;
);

lieg(v):=(
  erg=lig(v_1,v_2,v_3);
  erg=erg/|erg|;
  erg;
);

lig(v):=(
  erg=lig(v_1,v_2,v_3);
  erg;
);


drawliepoint(p,col,size):=(
  draw((p_3,p_4),size->size*2+2,color->col);
);

drawliecircle(p,col,size):=(
  fillcircle(((p_3,p_4)),|p_5|,size->size,color->col,alpha->.1);
//drawcircle(((p_3,p_4)),|p_5|,size->size,color->col);

);

drawlieline(p,col,size):=(
  draw(line((p_3,p_4,-p_1)),size->size*.3,color->col);
);


drawlie(p,col,siz):=(
  if(p_5~=0 & p_1~=-p_2, ,
   if(p_5~=0, drawliepoint(p/(p_1+p_2),col,siz),
    if(p_1~=-p_2, drawlieline(p/p_5,col,siz),
      drawliecircle(p/(p_1+p_2),col,siz))))
);

inf=(1,-1,0,0,0);
ee=(0,0,0,0,1);
xx=apply(1..5,round(random(100)))/100;
yy=apply(1..5,round(random(100)))/100;
orth(a,b,c,d):=(
   m=transpose([a,b,c,d]);
   [
      det([m_2,m_3,m_4,m_5]),
     -det([m_1,m_3,m_4,m_5]),
      det([m_1,m_2,m_4,m_5]),
     -det([m_1,m_2,m_3,m_5]),
      det([m_1,m_2,m_3,m_4])
   ]
);
liequad=[[-1,0,0,0,0],[0,1,0,0,0],[0,0,1,0,0],[0,0,0,1,0],[0,0,0,0,-1]];

roots(c,b,a):=(
p=b/a;
q=c/a;
d=sqrt(p^2/4-q);
[-p/2+d,-p/2-d];
);

findlie(a,b,c):=(
   aa=orth(liequad*a,liequad*b,liequad*c,xx);
   bb=orth(liequad*a,liequad*b,liequad*c,yy);
   r1=aa*liequad*aa;
   r2=2*bb*liequad*aa;
   r3=bb*liequad*bb;
   roo=roots(r1,r2,r3);
   ergs=if(isreal(roo_1),
     [aa+roo_1*bb,aa+roo_2*bb],
     [inf,inf]);
   ergs
);

mid(c):=((c_3,c_4)/(c_1+c_2));
rad(c):=(c_5/(c_1+c_2));
k(c):=(c_1+c_2)/c_5;

theother(a,b,c,dd):=(
//TODO:wenn ich dd ald d benenne wird der überschrieben. Das darf nicht sein!!!!!
  circs=findlie(rev(a),rev(b),rev(c));
//  out=if(|g(circs_1,dd)|<0.000000000001,circs_2,circs_1);
  out=if(|g(circs_1,dd)|<|g(circs_2,dd)|,circs_2,circs_1);
  out=out/|out|;
  out;
);



        </script>


        <script id='csmove' type='text/x-cindyscript'>
n=50;
        zoom=((Z.y-Z1.y)*2.8)^2+.1;

k1=0;
k2=1;
k3=1;



isgood(a,b,c):=(
  sq=sqrt(a*b+a*c+b*c);
  |sq-round(sq)|<0.000000001;
);

dd=|C1.x-C2.x|/n;
translate(off+Pos.xy-loc);
scale(zoom);


if(k1*k2*k3!=0,

 //Mittelpunktspositionen
 poa=(0,0);
 pob=(1/k1+1/k2,0);
 cr1=liec(poa.x,poa.y,1/k1+1/k3);
 cr2=liec(pob.x,pob.y,1/k2+1/k3);
 erg=findlie(cr1,cr2,ee);
 poc=mid(erg_1);
 if(area(poa,pob,poc)<0,poc=mid(erg_2));
 sp=(poa+pob+poc)/3;

 ca=liec(poa-sp,1/k1);
 cb=liec(pob-sp,1/k2);
 cc=liec(poc-sp,1/k3);
);

if(k1*k2*k3~=0,
  ks=sort([k1,k2,k3]);
  kk1=ks_1;
  kk2=ks_2;
  kk3=ks_3;
  if(kk2==0,
    ca=liec((0,0),1/kk3);
    cb=(lieg(cross((0,1/kk3,1),(1,0,0))));
    cc=rev(lieg(cross((0,-1/kk3,1),(1,0,0))));
  );
  if(kk2!=0,
    ks=[k1,k2,k3]--[0];
    kk2=ks_1;
    kk3=ks_2;
    s=if(kk2<kk3,1,-1);
    rsum=1/kk2+1/kk3;
    rdiff=1/kk2-1/kk3;

    offx=sqrt(rsum^2-rdiff^2)/2;
    offy=-rsum/2;

    ca=liec((offx,s*offy),1/kk2);

    cb=liec((-offx,offy*s),1/kk3);

    mi=if(1/kk2<1/kk3,1/kk2,1/kk3);


    vv=(cross((-offx,-offy-mi,1),(+offx,-offy-mi,1)));
    cc=rev(lig(vv));
  );

);









cd=rev(findlie(ca,cb,cc)_1);
ce=theother(ca,cb,cc,cd);

k4=k(cd);
k5=k(ce);
kmax=max((k1,k2,k3));
if(|k5|>kmax,
  hh=ce;
  ce=cd;
  cd=hh;
  k4=k(cd);
  k5=k(ce);
);


eps=0.15/zoom;
count=0;
ganzzahl=isgood(k1,k2,k3);

deeper(cir):=(
  rad=rad(cir);
  if(|rad|<eps,false,
    if((|mid(cir)*zoom|-|rad|*zoom)<18,
      true,true
    );
  );
);

dc(cir):=(
  drawlie(cir,(0.7,0,0),1);
  if(ganzzahl,  dc2(cir));
  count=count+1;
);

dc2(cir):=(

  kr=k(cir);
  if(kr>0,
    ra=rad(cir);
    mi=mid(cir);
    tsiz=(ra*zoom/12)^(1.05)*300;
    toff=(-.005,0)/zoom;
    if(kr>9,toff=(-tsiz*.007,0.01)/zoom);
    if(kr>99,toff=(-tsiz*.012,0.01)/zoom);
    drawtext(mi+toff,offset->(1-tsiz/2.5,-tsiz/2.5-1),round(1/ra),size->tsiz)
  );
  if(kr<0,
    ra=rad(cir);
    mi=mid(cir);
    tsiz=-ra*zoom*6;
    drawtext(mi+(ra,ra),round(1/ra),size->tsiz)
  );

  if(kr~=0,
    hom=(cir_3,cir_4,cir_2*zoom);
    pt=(join(hom,(1,0,0))).xy;
    if(pt.y<0,pt=pt-(0,1.5));
    if(pt.y>0,pt=pt+(0,0.5));
    tsiz=30;

  //  drawtext(pt.xy,0,size->tsiz)
  )
);


doit(a,b,c,d,n):=(
  regional(new);
  if(n>0,
   new=theother(a,b,c,d);
   dc(new);
   if(deeper(new),
  //  doit(a,b,new,c,n-1);
  doit(new,a,c,b,n-1);
    doit(new,b,c,a,n-1);
   );
  );
);

drawlie(ca,(0,0,0),4);
drawlie(cb,(0,0,0),4);
drawlie(cc,(0,0,0),4);
drawlie(cd,(0.8,0,0),4);
drawlie(ce,(0,0.6,0),4);


tiefe=round(|T.x-T1.x|*.7);
doit(ca,cb,cc,ce,tiefe);
if(isgood(k1,k2,k3),
  dc2(ca);
  dc2(cb);
  dc2(cc);
  dc2(cd);
  dc2(ce);
);
errc(count);


numbguess(x,t):=(

 if(x~=round(x)%t<1,
    [round(x),1],
    p=1/(x-floor(x));
    v=numbguess(p,t-1);
    [v_2+floor(x)*v_1,v_1];
  );
);

scale(1/zoom);
translate(-(off+Pos.xy-loc));


nn=round(|F,F1|)+1;
//if(nn>tiefe,nn=tiefe+1);
seq=[0];
apply(1..nn,ii,apply(ii..nn,jj,seq=seq++[ii/jj]));
seq=set(seq);
apply(seq,f,ff=f*zoom*2;
     draw((ff-zoom,0)+(off+Pos.xy-loc),(ff-zoom,-.5)+(off+Pos.xy-loc),color->(0,0,0));
     v=numbguess(f,9);
 //    text="$"+replace(guess(f),"/","\over ")+"$";

    o1=if(v_1>9,-0.15,0);
    o2=if(v_2>9,-0.15,0);
     drawtext((ff-zoom+.05-.2+o1,-1.2)+(off+Pos.xy-loc),v_1,size->12,align->"center");
     drawtext((ff-zoom+.05-.2+o2,-1.7)+(off+Pos.xy-loc),v_2,size->12,align->"center");
     draw((ff-zoom+.05-.3,-1.3)+(off+Pos.xy-loc),(ff-zoom+.05+.1,-1.3)+(off+Pos.xy-loc),color->(0,0,0));
);




        </script>


        <div  id="CSCanvas" style="width:800px; height:600px; border:2px solid #000000"></div>
        <br>
        <br>
        <br>

        <script type="text/javascript">

            var gslp=[


                      {name:"Z1", type:"Free", pos:[20,-7],color:[1,1,1],size:0.1,pinned:true},
                      {name:"Z2", type:"Free", pos:[20,-1],color:[1,1,1],size:0.1,pinned:true},
                      {name:"z", type:"Segment", args:["Z1","Z2"],color:[0,0,0]},
                      {name:"T1", type:"Free", pos:[-5,-2],color:[1,1,1],size:0.1,pinned:true},
                      {name:"T2", type:"Free", pos:[18,-2],color:[1,1,1],size:0.1,pinned:true},
                      {name:"t", type:"Segment", args:["T1","T2"],color:[0,0,0]},
                      {name:"F1", type:"Free", pos:[-5,-4],color:[1,1,1],size:0.1,pinned:true},
                      {name:"F2", type:"Free", pos:[18,-4],color:[1,1,1],size:0.1,pinned:true},
                      {name:"f", type:"Segment", args:["F1","F2"],color:[0,0,0]},
                      {name:"A1", type:"Free", pos:[-8,-6],color:[1,1,1],size:0.1},

                      {name:"Z", type:"PointOnSegment", args:["z"], pos:[20,-6],color:[1,1,1],size:4,narrow:true},
                      {name:"T", type:"PointOnSegment", args:["t"], pos:[-5,-2],color:[1,1,1],size:4,narrow:true},
                      {name:"F", type:"PointOnSegment", args:["f"], pos:[-5,-4],color:[1,1,1],size:4,narrow:true},

                      {name:"Pos", type:"Free", pos:[0,0],color:[1,1,0],size:.001,alpha:0}


                      ];
            CindyJS({canvasname:"CSCanvas",
                        defaultAppearance: {clip: "inci", overhangLine: 1.2},
                        movescript:"csmove",
                        initscript:"init",
                        mouseupscript:"up",
                        mousedownscript:"down",
                        geometry:gslp});

        </script>

	</body>
</html>
